package io.renren.modules.work.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.modules.basic.controller.MtCcTaskRunInfoDetController;
import io.renren.modules.basic.entity.MtCcTaskRunInfoDetEntity;
import io.renren.modules.basic.entity.MtCcTaskRunInfoEntity;
import io.renren.modules.basic.service.MtCcTaskRunInfoDetService;
import io.renren.modules.basic.service.MtCcTaskRunInfoService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.websocket.WebSocketServer;
import io.renren.modules.work.entity.WorkEntity;
import io.renren.modules.work.service.WorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: weihongyang
 * @Date: 2019/10/14 9:59
 * @Version 1.0
 */
@RestController
@Api(description = "出库任务")
@RequestMapping("/work/controller")
public class WorkController extends AbstractController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private WorkService workService;
    @Autowired
    private MtCcTaskRunInfoService mtCcTaskRunInfoService;
    @Autowired
    private MtCcTaskRunInfoDetService mtCcTaskRunInfoDetService;
    @Autowired
    private MtCcTaskRunInfoDetController mtCcTaskRunInfoDetController;

    @ApiOperation(value = "手动下总任务" , notes = "手动下总任务")
    @PostMapping("/check/out/for/main/task")
    public R checkOutForMainTask(@RequestBody Map<String, Object> params){

        params.put("userId",getUserId());
        params.put("userName",getUser().getUsername());
        workService.checkOutForMainTask(params);
        return R.ok();
    }

    @ApiOperation(value = "出库总任务下发" , notes = "出库总任务下发")
    @PostMapping("/check/out/main/task/for/det")
    public R checkOutMainTaskForDet(@RequestBody Map<String, Object> params){

        params.put("doorWay",getUser().getDoorWay());
        return workService.checkOutMainTaskForDet(params);
    }

    @ApiOperation(value = "出库子任务执行" , notes = "出库子任务执行")
    @PostMapping("/execute/issue/task")
    public R executeIssueTask(@RequestBody Map<String, Object> params){

        workService.executeIssueTask(params);

        return R.ok();
    }

    @ApiOperation(value = "出库任务自动执行" , notes = "出库任务自动执行")
    @RequestMapping("/auto/run/out/task/{id}")
    public R autoRunOutTask(@PathVariable("id") Integer id) throws Exception {
        MtCcTaskRunInfoEntity mtCcTaskRunInfoEntity = mtCcTaskRunInfoService.getById(id);
        //当前主任务是否是正在执行状态
        if (mtCcTaskRunInfoEntity.getStatus()==1){
            while (true){
                //当前主任务是否是正在执行状态,重置按钮会重置主任务状态,若不判断,会冲突
                if (mtCcTaskRunInfoService.getById(id).getStatus()==1){
                    //当前是否有子任务正在执行
                    List<MtCcTaskRunInfoDetEntity> status = mtCcTaskRunInfoDetService
                            .list(new QueryWrapper<MtCcTaskRunInfoDetEntity>()
                                    .eq("status", 1));
                    if (status.size()==0){
                        //取出当前主任务下未执行的子任务,按时间升序排
                        List<MtCcTaskRunInfoDetEntity> list1 = mtCcTaskRunInfoDetService.list(new QueryWrapper<MtCcTaskRunInfoDetEntity>()
                                .eq("task_run_id", id)
                                .eq("status", 0)
                                .orderByAsc("create_time"));
                        //是否存在未执行的子任务
                        if (list1.size()!=0){
                            MtCcTaskRunInfoDetEntity mtCcTaskRunInfoDetEntity = list1.get(0);
                            //MtCcTaskRunInfoDetController mtCcTaskRunInfoDetController=new MtCcTaskRunInfoDetController();
                            R r = mtCcTaskRunInfoDetController.runTaskInfoDet(mtCcTaskRunInfoDetEntity.getId());
                            int code = Integer.parseInt(String.valueOf(r.get("code")));
                            //返回状态值不等于200时,代表执行失败
                            if (code!=0){
                                String msg=String.valueOf(r.get("msg"));
                                String s="本次出库操作对应平台存在障碍物,请移除平台上的障碍物后再执行本条子任务!";
                                //平台上有障碍物,线程等待10s
                                if (s.equals(msg)){
                                    Thread.sleep(1000*10);
                                }else {
                                    return R.error(msg);
                                }
                            }
                        }else {
                            logger.info("本条主任务下的所有子任务已执行完毕.");
                            return R.ok("本条主任务下的所有子任务已执行完毕.");
                        }
                    }else {
                        logger.info("当前有其他子任务正在执行,自动执行中断!");
                        return R.error("当前有其他子任务正在执行,自动执行中断!");
                    }
                }else {
                    logger.info("当前主任务不是执行状态,请点击执行任务后重试!");
                    return R.error("当前主任务不是执行状态,请点击执行任务后重试!");
                }
            }
        }else {
            logger.info("当前主任务不是执行状态,请点击执行任务后重试!");
            return R.error("当前主任务不是执行状态,请点击执行任务后重试!");
        }
    }

}
